/*
 * GameStartedGameStateController.h
 *
 *  Created on: Dec 29, 2011
 *      Author: rudi
 */

#ifndef GAMESTARTEDGAMESTATECONTROLLER_H_
#define GAMESTARTEDGAMESTATECONTROLLER_H_
#include <wx/wx.h>
#include "IGameStateController.h"
#include "GameStartedSubStateController.h"
#include "SmartPointer.h"
#include "WxUtils.h"

class wxSocketEvent;

/**
 * This class handles the events when the game is started
 *
 * @see IGameStateController
 * @author Rudolf Heszele heszele@gmail.com
 * @version 1.0
 * @since 2012-03-08
 */
class GameStartedGameStateController: public IGameStateController
{
	public:
		/**
		 * Helper class to change the SubState of the GameStartedStateController object
		 *
		 * This class instructs wxWidgets to do an async callback, and in this async callback
		 * it changes the SubState of the GameStartedStateController object
		 * @see WxUtils::ITask
		 */
		class ChangeSubStateTask: public WxUtils::ITask
		{
			public:
				/**
				 * The only constructor takes the needed parameters to perform the state change
				 *
				 * @param gameStartedController The object to perform the state change on
				 * @param subStateController The new SubState, which needs to be set
				 */
				ChangeSubStateTask(GameStartedGameStateController* gameStartedController, SmartPointer<GameStartedSubStateController> subStateController);
				virtual ~ChangeSubStateTask();
				/**
				 * @see WxUtils::ITask::execute
				 */
				virtual void execute();

			private:
				GameStartedGameStateController* mGameStartedController;
				SmartPointer<GameStartedSubStateController> mSubStateController;
		};
		/**
		 * Make the class friend, to reach the private data members
		 */
		friend class ChangeSubStateTask;
		/**
		 * This constructor is used when the game is initially started - when a client is connected to a server
		 *
		 * @param subStateController The initial SubState of the object
		 */
		GameStartedGameStateController(SmartPointer<GameStartedSubStateController> subStateController);
		/**
		 * This constructor is used when one of the users wants to restart the game
		 *
		 * @param isRestartFromUser true if the restart request is coming from the user, false it is from the remote side
		 */
		GameStartedGameStateController(bool isRestartFromUser);
		virtual ~GameStartedGameStateController();

		// Methods inherited from Controller

		/**
		 * @see IController::handleMouseDown
		 */
		virtual void handleMouseDown(const wxPoint& position);
		/**
		 * @see IController::handleMouseUp
		 */
		virtual void handleMouseUp(const wxPoint& position);
		/**
		 * @see IController::handleMouseMotion
		 */
		virtual bool handleMouseMotion(const wxPoint& position);
		/**
		 * @see IController::getBitmapToPaint
		 */
		virtual const wxBitmap& getBitmapToPaint() const;
		/**
		 * @see IController::getBitmapToDraw
		 */
		virtual wxBitmap& getBitmapToDraw();

		// Methods inherited fromGameStateController

		/**
		 * @see IGameStateContrller::handleSocketConnection
		 */
		virtual void handleSocketConnection(wxSocketEvent& socketEvent);
		/**
		 * @see IGameStateContrller::handleSocketLost
		 */
		virtual void handleSocketLost(wxSocketEvent& socketEvent);
		/**
		 * @see IGameStateContrller::handleSocketInput
		 */
		virtual void handleSocketInput(wxSocketEvent& socketEvent);
		/**
		 * @see IGameStateContrller::handleSocketOutput
		 */
		virtual void handleSocketOutput(wxSocketEvent&);

		// Own methods

		/**
		 * This method changes the SubState of the object in an async way
		 *
		 * @param subStateController The new SubState
		 */
		void changeSubState(GameStartedSubStateController* subStateController);
		/**
		 * Checks if the SubState is local turn or not
		 *
		 * @return true if the SubState is local turn, false otherwise
		 */
		bool isLocalTurn();

	private:
		/**
		 * The SubState of the game after it is started - currently can be remote or local
		 */
		SmartPointer<GameStartedSubStateController> mSubStateController;
};

inline bool GameStartedGameStateController::isLocalTurn()
{
	return mSubStateController->getSubstateId() == GameStartedSubStateController::LOCAL_TURN;
}

#endif /* GAMESTARTEDGAMESTATECONTROLLER_H_ */
